package com.lemon.cloud.websocket.config;

import com.lemon.cloud.websocket.distribute.MessageDistributor;
import com.lemon.cloud.websocket.distribute.RedisMessageDistributor;
import com.lemon.cloud.websocket.distribute.RedisWebsocketMessageListener;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;


/**
 * @author HuangDS
 */
@ConditionalOnClass({RedisTemplate.class})
@ConditionalOnProperty(
        prefix = "websocket",
        name = {"message-distributor"},
        havingValue = "redis",
        matchIfMissing = true
)
@Configuration(
        proxyBeanMethods = false
)
public class RedisMessageDistributorConfiguration {

    @Bean
    @ConditionalOnMissingBean({MessageDistributor.class})
    public RedisMessageDistributor messageDistributor(@Autowired RedisTemplate<String,String> redisTemplate) {
        return new RedisMessageDistributor(redisTemplate);
    }

    @Bean
    @ConditionalOnBean({RedisMessageDistributor.class})
    @ConditionalOnMissingBean
    public RedisWebsocketMessageListener redisWebsocketMessageListener(@Autowired RedisTemplate<String,String> redisTemplate) {
        return new RedisWebsocketMessageListener(redisTemplate);
    }

    @Bean
    @ConditionalOnBean({RedisMessageDistributor.class})
    @ConditionalOnMissingBean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }

    @Configuration(
            proxyBeanMethods = false
    )
    @ConditionalOnMissingBean({MessageDistributor.class})
    static class RedisMessageListenerRegisterConfiguration {
        private final RedisMessageListenerContainer redisMessageListenerContainer;
        private final RedisWebsocketMessageListener redisWebsocketMessageListener;

        @PostConstruct
        public void addMessageListener() {
            this.redisMessageListenerContainer.addMessageListener(this.redisWebsocketMessageListener, new PatternTopic(MessageDistributor.DISTRIBUTOR_KEY));
        }

        public RedisMessageListenerRegisterConfiguration(final RedisMessageListenerContainer redisMessageListenerContainer, final RedisWebsocketMessageListener redisWebsocketMessageListener) {
            this.redisMessageListenerContainer = redisMessageListenerContainer;
            this.redisWebsocketMessageListener = redisWebsocketMessageListener;
        }
    }
}
